<template>
	<div class="addVideo">
		<el-dialog
			title="赠送名额"
			:visible.sync="dialogVisible"
			width="600px"
			:modal-append-to-body="false"
			:close-on-click-modal="false"
			custom-class="custom-dialog"
		>
			<el-form
				ref="forms"
				size="small"
				:model="forms"
				:rules="rules"
				label-width="140px"
				label-position="right"
			>
				<el-form-item label="免费名额：" prop="free_num">
					<el-input
						v-model.number="forms.free_num"
						placeholder="请输入免费名额数量"
						clearable
						style="width: 100%"
					/>
				</el-form-item>
				<el-form-item label="折扣名额：" prop="discount_num">
					<el-input
						v-model.number="forms.discount_num"
						placeholder="请输入折扣名额数量"
						clearable
						style="width: 100%"
					/>
				</el-form-item>
				<el-form-item label="折扣金额：" prop="discount_money">
					<el-input
						v-model="forms.discount_money"
						placeholder="请输入折扣金额"
						clearable
						style="width: 100%"
					/>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button style="margin-right: 5px" @click="dialogVisible = false">取消</el-button>
				<el-button type="primary" @click="saveVideos">保存</el-button>
			</span>
		</el-dialog>
	</div>
</template>
<script>
	import { mapGetters } from 'vuex'
	import { sendQuota } from '@/api/user-manager.js'
	export default {
		name: 'addSponsor',
		data() {
			const validatePass = (rule, value, callback) => {
				const regex = /^(\d+\.?\d{0,2}|\.\d{1,2})$/
				if (this.forms.contact_tel && !value) {
					return callback(new Error('请输入折扣金额'))
				} else if (value && !regex.test(value)) {
					return callback(new Error('格式错误'))
				}
				callback()
			}
			return {
				dialogVisible: false,
				disabled: false,
				forms: {
					title: '',
					content: ''
				},
				rules: {
					free_num: [
						{ required: true, message: '请输入免费名额数量', trigger: 'change' },
						{ pattern: /^0|[1-9]\d*$/, message: '格式不正确', trigger: 'blur' }
					],
					discount_num: [
						{ required: true, message: '请输入折扣名额数量', trigger: 'change' },
						{ pattern: /^0|[1-9]\d*$/, message: '格式不正确', trigger: 'blur' }
					],
					discount_money: [{ required: true, validator: validatePass, trigger: 'change' }]
				},
				columnOptions: [],
				info: {}
			}
		},
		computed: {
			...mapGetters(['token', 'userInfo'])
		},
		methods: {
			async initData(info = {}) {
				this.forms = {
					spon_id: info.spon_id,
					race_id: info.race_id,
					discount_money: '',
					free_num: '',
					discount_num: ''
				}
				this.dialogVisible = true
				this.$nextTick(() => {
					this.$refs.forms.clearValidate()
				})
			},
			// 保存
			async saveVideos() {
				this.$refs.forms.validate(async valid => {
					if (valid) {
						const loading = this.$setLoading({
							lock: true,
							text: '数据提交中~'
						})
						Promise.all([sendQuota(this.forms)])
							.then(() => {
								this.$message.success('设置成功！')
								this.dialogVisible = false
								this.$parent.loadData()
							})
							.finally(() => loading.close())
					}
				})
			}
		}
	}
</script>
<style lang="scss" scoped>
	.addVideo {
		::v-deep {
			.el-dialog .el-dialog__body {
				padding-bottom: 20px;
			}
			.el-form .el-form-item {
				margin-bottom: 16px;
			}
		}
		.box-wrap {
			display: flex;
			align-items: center;
			flex-wrap: wrap;
		}
	}
</style>
